//
//  TextInterface.cpp
//  Dungeons and Princesses
//
//  Created by John Grange on 10/22/11.
//  Copyright (c) 2011 SD Networks. All rights reserved.
//

#include <iostream>
#include "TextInterface.h"
#include <string.h>
#include "Utility.h"
#include <algorithm>

TextInterface::TextInterface(Game *ptrToCurrentGame) : Interface(ptrToCurrentGame) {
    
    std::cout << VERSION << endl;
    std::cout << WELCOMEMSG << endl;
    
    
}


void TextInterface::loadInterface() {
    
    update("");
    
}
void TextInterface::update(string errorMSG) {
    
    if (errorMSG != "") {
        cout << endl << errorMSG << endl << endl;
    }
    showDescription();
    showDirections();
    showRoomInventory();
    showPrompt();
    
}

void TextInterface::setupPlayer() {
    
    std::cout << "\nPlease enter your character name: " << flush;
    
    string playerName;
    
    getline(cin, playerName);
    
    playerName[0] = toupper(playerName[0]);
    
    gamePtr->setupPlayer(playerName);
    
}
void TextInterface::showRoomInventory() {
    
    const vector<Listing> * roomInv = gamePtr->getCurrentRoom()->getManifest();
    
    if (roomInv->size() > 0) {
        std::cout << "\n" << "In the room you see the following : " << endl;
        for_each(roomInv->begin(), roomInv->end(), Utility::printListingOBject);
        
        std::cout << endl;
    }
}

void TextInterface::showDescription() {
    
    cout << endl << endl;
    
    cout << gamePtr->getCurrentRoom()->Base::getDesc();
}

void TextInterface::showPrompt() {
    
    string command;
    cout << endl << "------------------------------------" << endl << gamePtr->getCurrentPlayer()->getDesc() << ", what do you wish to do ?   ";
    getline (cin,command);
    
    gamePtr->executeCommand(command);
    
}

void TextInterface::showPlayerInventory() {
    
    const vector<Listing> * playerInv = gamePtr->getCurrentPlayer()->inventory.getManifest();
    
    if (playerInv->size() > 0) {
        std::cout << "\n" << "You have in your inventory the following items : " << endl << endl;
        for_each(playerInv->begin(), playerInv->end(), Utility::printListingOBject);
        
        std::cout << endl;
        
        update();
    }
    
    else {
        update("You do not have anything in your inventory");
    }
}

void TextInterface::showHelp(vector<std::string> *helpFile) {
    
    vector<std::string>::iterator helpLine;
    
    std::cout << endl << endl;
    for (helpLine = helpFile->begin(); helpLine != helpFile->end(); helpLine++) {
        std::cout << *helpLine << endl;
    }
    
    update();
}
void TextInterface::getResponse(std::string prompt, std::string &response) {
    
    
        cout << prompt << flush;
        getline(cin, response);
        
        Utility::removeGarbage(response);
        Utility::lowerCase(response);
            
}

void TextInterface::showDirections() {
    
    for (int directionNum = 0; directionNum < 4; directionNum++) {
        cout << gamePtr->getCurrentRoom()->getDirections()[directionNum].getDesc() << endl;
    }
}

